package interview;

import java.util.HashSet;

public class interview_05_02 {
    public String printBin(double num) {
        HashSet<String> hashSet =new HashSet<>();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("0.");
        while(!hashSet.contains(num+"")){
            hashSet.add(num+"");
            num*=2;
            if(num-1>0){
                num-=1;
                stringBuilder.append('1');
            }
            else if(num-1<=0&&num-1>=0){
                num-=1;
                stringBuilder.append('1');
                break;
            }
            else{
                stringBuilder.append('0');
            }
        }
        if(stringBuilder.toString().length()>32) return "ERROR";
        if(Math.abs(num)<0.000001) return stringBuilder.toString();
        return "ERROR";
    }
}
